﻿#include "scannercontrol.h"

ScannerControl::ScannerControl(QObject *parent)
    : QObject{parent}
{
    scannerSerial = new QSerialPort();

}

ScannerControl::~ScannerControl()
{

}

void ScannerControl::setConnectParameter(QString _portName, QString _baudRate,
                                      QString _dataBite, QString _parity, QString _stopBite)
{

    setPortName(_portName);
    setBaudRate(_baudRate);
    setDataBite(_dataBite);
    setParity(_parity);
    setStopBite(_stopBite);

    QSerialPort::BaudRate set_BaudRate;
    QSerialPort::DataBits set_DataBite;
    QSerialPort::Parity   set_Parity;
    QSerialPort::StopBits set_StopBite;


    if("115200" == BaudRate()){
        set_BaudRate = QSerialPort::Baud115200;
    }else if("57600" == BaudRate()){
        set_BaudRate = QSerialPort::Baud57600;
    }else if("38400" == BaudRate()){
        set_BaudRate = QSerialPort::Baud38400;
    }else if("19200" == BaudRate()){
        set_BaudRate = QSerialPort::Baud19200;
    }else if("9600" == BaudRate()){
        set_BaudRate = QSerialPort::Baud9600;
    }else if("4800" == BaudRate()){
        set_BaudRate = QSerialPort::Baud4800;
    }else if("2400" == BaudRate()){
        set_BaudRate = QSerialPort::Baud2400;
    }else {
        set_BaudRate = QSerialPort::Baud1200;
    }

    if("8" == DataBite()){
        set_DataBite = QSerialPort::Data8;
    }else if("7" == DataBite()){
        set_DataBite = QSerialPort::Data7;
    }else if("6" == DataBite()){
        set_DataBite = QSerialPort::Data6;
    }else {
        set_DataBite = QSerialPort::Data5;
    }

    if("None" == Parity()){
        set_Parity = QSerialPort::NoParity;
    }else if("Even" == Parity()){
        set_Parity = QSerialPort::EvenParity;
    }else if("Odd" == Parity()){
        set_Parity = QSerialPort::OddParity;
    }else if("Mark" == Parity()){
        set_Parity = QSerialPort::MarkParity;
    }else {
        set_Parity = QSerialPort::SpaceParity;
    }

    if("1" == StopBite()){
        set_StopBite = QSerialPort::OneStop;
    }else if("1.5" == StopBite()){
        set_StopBite = QSerialPort::OneAndHalfStop;
    }else{
        set_StopBite = QSerialPort::TwoStop;
    }


    scannerSerial->setPortName(PortName());           // 设置串口的 串口号
    scannerSerial->setBaudRate(set_BaudRate);        // 设置串口的 波特率
    scannerSerial->setDataBits(set_DataBite);   		// 设置串口的 数据位
    scannerSerial->setParity(set_Parity);            // 设置串口的 校验位
    scannerSerial->setStopBits(set_StopBite);   		// 设置串口的 停止位
}

void ScannerControl::linkToComputer()
{
    if(scannerSerial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Cpp -- 扫码枪连接成功";
        setLinkState(true);
    }
    else
    {
        qDebug()<<"Cpp -- 扫码枪连接失败";
        setLinkState(false);
    }
}

bool ScannerControl::linkState() const
{
    return m_linkState;
}

void ScannerControl::setLinkState(bool newLinkState)
{
    if (m_linkState == newLinkState)
        return;
    m_linkState = newLinkState;
    emit linkStateChanged();
}

QString ScannerControl::PortName() const
{
    return m_PortName;
}

void ScannerControl::setPortName(const QString &newPortName)
{
    if (m_PortName == newPortName)
        return;
    m_PortName = newPortName;
    emit PortNameChanged();
}

QString ScannerControl::BaudRate() const
{
    return m_BaudRate;
}

void ScannerControl::setBaudRate(const QString &newBaudRate)
{
    if (m_BaudRate == newBaudRate)
        return;
    m_BaudRate = newBaudRate;
    emit BaudRateChanged();
}

QString ScannerControl::DataBite() const
{
    return m_DataBite;
}

void ScannerControl::setDataBite(const QString &newDataBite)
{
    if (m_DataBite == newDataBite)
        return;
    m_DataBite = newDataBite;
    emit DataBiteChanged();
}

QString ScannerControl::Parity() const
{
    return m_Parity;
}

void ScannerControl::setParity(const QString &newParity)
{
    if (m_Parity == newParity)
        return;
    m_Parity = newParity;
    emit ParityChanged();
}

QString ScannerControl::StopBite() const
{
    return m_StopBite;
}

void ScannerControl::setStopBite(const QString &newStopBite)
{
    if (m_StopBite == newStopBite)
        return;
    m_StopBite = newStopBite;
    emit StopBiteChanged();
}


bool ScannerControl::scannerType() const
{
    return m_scannerType;
}

void ScannerControl::setScannerType(bool newScannerType)
{
    if (m_scannerType == newScannerType)
        return;
    m_scannerType = newScannerType;
    emit scannerTypeChanged();
}

QString ScannerControl::scanResult() const
{
    return m_scanResult;
}

void ScannerControl::setScanResult(const QString &newScanResult)
{
    if (m_scanResult == newScanResult)
        return;
    m_scanResult = newScanResult;
    emit scanResultChanged();
}
